/*
 * 商户
 *
 * */

import Api from "../../api/api"
import * as types from "../mutation-types"
import  {setParams} from "../../util/methods"


const state = {
    params: {
        "store_name": "",
        "store_no": "",
        "store_input_time_start": "",
        "store_input_time_end": "",
        "store_city": "",
        "store_status": "",
        "page": 0
    },
    storeList: [],
    storeDetail: '',
    url: ''
}

const actions = {
    storeList({commit}, payload){
        return new Promise((resolve, reject) => {
            var append = payload.hasOwnProperty("append") ? payload.append : false
            Api.storeList(state.params)
                .then(res => {
                    commit({
                        type: "STORE_LIST",
                        append: append,
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })


        })

    },
    storeDetail({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.storeDetail({"store_no": payload.store_no})
                .then(res => {
                    commit({
                        type: "STORE_D",
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })


        })
    },
    //获取二维码
    storeQcode({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.storeQcode({"store_no": payload.store_no})
                .then(res => {
                    commit({
                        type: "setQcodeImg",
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })


        })
    },
}


const mutations = {
    [types.STORE_LIST](state, payload){
        if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
            state.storeList = payload.res.data;
        } else {
            state.storeList = state.storeList.concat(payload.res.data)
        }
    },
    setTime(state, payload){
        state.params[payload.timeType] = payload.time
    },
    setParamStatus(state, payload){
        state.params.status = payload.status
    },
    [types.STORE_D](state, payload){
        state.storeDetail = payload.res
    },
    setQcodeImg(state, payload){
        state.url = payload.res.data.url
    },
    setPage(state, payload){
        state.params.page = payload.page
    },
    setCity(state, payload){
        state.params.store_city = payload.city
    },
    setMerchantParams(state, payload){
        state = setParams(state, payload.params)
    },
    resetParam(state){
        state.params = {
            "store_name": "",
            "store_no": "",
            "store_input_time_start": "",
            "store_input_time_end": "",
            "store_city": "",
            "store_status": "",
            "page": 0
        };
        state.storeList = [];
        state.storeDetail = '';
        state.url = '';
    }
}

export default{
    state,
    actions,
    mutations
}